package com.xmy.dao.Impl;

import com.xmy.dao.BaseDao;
import com.xmy.dao.PetDao;
import com.xmy.entity.Pet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PetDaoImpl extends BaseDao implements PetDao {
    @Override
    public List<Pet> SelPet() {
        List<Pet> pets = new ArrayList<Pet>();
        String sql = "select petid,name from pet";
        ResultSet rs = executeQuery(sql,null);
        try{
            while (rs.next()){
                Pet pet = new Pet();
                pet.setPetid(rs.getInt(1));
                pet.setName(rs.getString(2));
                pets.add(pet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }

    @Override
    public Pet SelPetById(int id) {
        String sql = "select * from pet where petid = ?";
        Object[] objects = {id};
        ResultSet rs = null;
        Pet pet = null;
        try{
            pet = new Pet();
            rs = executeQuery(sql,objects);
            rs.next();
            pet.setPetid(rs.getInt(1));
            pet.setName(rs.getString(2));
            pet.setMid(rs.getInt(3));
            pet.setSid(rs.getInt(4));
            pet.setType(rs.getString(5));
            pet.setHealth(rs.getInt(6));
            pet.setLove(rs.getInt(7));
            pet.setPrice(rs.getInt(9));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pet;
    }

    @Override
    public int UpdatePet(int id) {
        String sql = "update pet set mid = null where id = ?";
        Object[] objects = {id};
        return executeUpdate(sql,objects);
    }
}
